package open.tech.dawn.rest.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import open.tech.dawn.core.error.ServiceException;
import open.tech.dawn.rest.common.log.LogManager;
import open.tech.dawn.rest.common.log.factory.LogTaskFactory;
import open.tech.dawn.rest.common.util.RequestUtil;
import open.tech.dawn.rest.module.system.enums.ManagerStatus;
import open.tech.dawn.rest.module.system.model.SysUser;
import open.tech.dawn.rest.module.system.service.ISysMenuService;
import open.tech.dawn.rest.module.system.service.ISysUserService;
import open.tech.dawn.web.core.entity.LoginUser;
import open.tech.dawn.web.core.service.ILoginUserService;
import open.tech.dawn.web.util.HttpKit;
import open.tech.dawn.web.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginUserServiceImpl implements ILoginUserService {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysMenuService sysMenuService;


    public LoginUser shiroUser(SysUser user) {
        LoginUser shiroUser = new LoginUser();
        shiroUser.setId(user.getId());
        Set<String> permissions = new HashSet<>();
        Integer[] roleArray = Convert.toIntArray(user.getRoleid());
        for (int roleId : roleArray) {
            if (0 != roleId) {
                List<String> resUrlsByRoleId = sysMenuService.getResUrlsByRoleId(roleId);
                if (CollUtil.isNotEmpty(resUrlsByRoleId)) {
                    permissions.addAll(resUrlsByRoleId);
                }
            }
        }
        shiroUser.setPermissions(permissions);
        return shiroUser;
    }


    @Override
    public LoginUser login(HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonBody = RequestUtil.getJSONBody(request);
        String account = jsonBody.getString("username");
        String password = jsonBody.getString("password");
        if (account == null || password == null) {
            throw new ServiceException("请输入账号/密码");
        }
        SysUser user = sysUserService.getByAccount(account);
        // 账号不存在
        if (null == user) {
            throw new ServiceException("账号不存在");
        }
        // 账号被冻结
        if (user.getStatus() != ManagerStatus.OK.getCode()) {
            LogManager.me().executeLog(LogTaskFactory.loginLog(user.getId(), "账号被冻结", HttpKit.getIp()));
            throw new ServiceException("账号被冻结");
        }
        // 比较密码
        if (!user.getPassword().equals(MD5Util.encrypt(password, user.getSalt()))) {
            LogManager.me().executeLog(LogTaskFactory.loginLog(user.getId(), "密码输入有误", HttpKit.getIp()));
            throw new ServiceException("密码输入有误");
        }
        LoginUser loginUser = shiroUser(user);
        LogManager.me().executeLog(LogTaskFactory.loginLog(loginUser.getId(), HttpKit.getIp()));
        return loginUser;
    }

    @Override
    public void logout(LoginUser loginUser, HttpServletRequest request, HttpServletResponse response) {

        LogManager.me().executeLog(LogTaskFactory.exitLog(loginUser.getId(), HttpKit.getIp()));
    }
}
